package org.jackysoft.entity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NamedNativeQueries;
import org.hibernate.annotations.NamedNativeQuery;
import org.jackysoft.entity.AbstractEntity;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Role;
import org.jackysoft.entity.User;
import org.jackysoft.util.MySqlSchema;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

@Entity
@Table(schema=MySqlSchema.Businessbase)
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
@NamedNativeQueries({
		@NamedNativeQuery(name = "getOwners", query = "select rus.role_id role_id,dus.department_id , '123456' storePass,'0' sortNum, 'true' enabled, ou.owners_name name ,su.localName localName"
				+ " from  viewfile f"
				+ " left join user_file_owner ou  on ou.viewfile_id=f.id"
				+ " inner join sysuser su "
				+ " left join roleUsers rus on rus.user_id=su.name"
				+ " left join departmentUsers dus on dus.user_id =su.name"
				+ " where su.name=ou.owners_name and f.id=:fida"
				+ " union "
				+ " select  rus.role_id role_id,dus.department_id,'123456' storePass,'0' sortNum, 'true' enabled,  ru.readers_name name,su.localName localName"
				+ " from  viewfile f"
				+ " left join user_file_reader ru on ru.viewfile_id=f.id"
				+ " inner join sysuser su "
				+ " left join roleUsers rus on rus.user_id=su.name"
				+ " left join departmentUsers dus on dus.user_id =su.name"
				+ " where su.name=ru.readers_name and f.id=:fidb", resultClass = User.class)

})
@SqlResultSetMapping(name = "viewfilerst", entities = @EntityResult(entityClass = org.jackysoft.entity.ViewFile.class, fields = {
		@FieldResult(name = "id", column = "f.id"),
		@FieldResult(name = "name", column = "f.name"),
		@FieldResult(name = "size", column = "f.size"),
		@FieldResult(name = "mimeType", column = "f.mimeType"),
		@FieldResult(name = "extision", column = "f.extision"),
		@FieldResult(name = "updateDate", column = "f.updateDate")

}))
public class ViewFile extends AbstractEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -95338151333001983L;
	protected String code;	
	protected String name;
	protected String realName;
	protected String realPath;
	protected String urlPrefix;
	protected long size;
	protected String mimeType;
	protected String extision;
	protected Calendar updateDate;
	protected Collection<User> owners = Lists.newArrayList();
	protected Collection<User> readers = Lists.newArrayList();
	private Set<User> readereds = Sets.newHashSet();
	private Collection<Role> roles = new ArrayList<Role>();
	private Collection<Department> departments = Lists.newArrayList();
	
	private CustomOrganization customOrganization;
	private User manager;

	public ViewFile() {

	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getRealPath() {
		return realPath;
	}

	public void setRealPath(String realPath) {
		this.realPath = realPath;
	}

	public String getUrlPrefix() {
		return urlPrefix;
	}

	public void setUrlPrefix(String urlPrefix) {
		this.urlPrefix = urlPrefix;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public String getMimeType() {
		return mimeType;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}

	public String getExtision() {
		return extision;
	}

	public void setExtision(String extision) {
		this.extision = extision;
	}

	public Calendar getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Calendar updateDate) {
		this.updateDate = updateDate;
	}

	/**
	 * 一个文件有多个用户
	 * */
	@ManyToMany
	@JoinTable(name = "user_file_owner")
	@Fetch(FetchMode.SELECT)
	public Collection<User> getOwners() {
		Collection<User> towners = Sets.newHashSet();
		return owners == null ? towners : owners;
	}

	public void setOwners(Collection<User> owners) {
		this.owners = owners;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * 一个文件有多个用户所有
	 * */
	@Transient
	public Collection<Role> getRoles() {
		return roles;
	}

	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}

	/**
	 * 一个文件有多个用户所有
	 * */
	@Transient
	public Collection<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(Collection<Department> departments) {
		this.departments = departments;
	}

	@Transient
	public CustomOrganization getCustomOrganization() {
		return customOrganization;
	}

	public void setCustomOrganization(CustomOrganization customOrganization) {
		this.customOrganization = customOrganization;
	}

	@ManyToMany
	@Fetch(FetchMode.SELECT)
	@JoinTable(name = "user_file_reader")
	public Collection<User> getReaders() {
		return readers;
	}

	public void setReaders(Collection<User> readers) {
		this.readers = readers;
	}

	public void setManager(User manager) {
		this.manager = manager;
	}

	@ManyToOne
	@PrimaryKeyJoinColumn
	@Fetch(FetchMode.SELECT)
	public User getManager() {
		return manager;

	}

	public void setReadereds(Set<User> readereds) {
		this.readereds = readereds;
	}

	@ManyToMany
	@Fetch(FetchMode.SELECT)
	@JoinTable(name = "user_file_readered")
	public Set<User> getReadereds() {
		return readereds;
	}

}
